package com.wcs.spring.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Repository;

import com.wcs.spring.entity.ClassTable;
import com.wcs.spring.entity.OrderClass;
import com.wcs.spring.mapper.ClassTableMapper;
import com.wcs.spring.service.ClassTableService;
import com.wcs.spring.util.PageUtil;
import com.wcs.spring.util.WeekDateUtil;

/**
 * <p>
 * Title: Phicomm_[子系统统名]_[模块名]
 * </p>
 * <p>
 * Description: [描述该类概要功能介绍]
 * </p>
 * 
 * @author hexuan2014
 * @version $Revision$ 2015-6-15
 * @author (lastest modification by $Author$)
 * @since 1.0
 */
@Repository(value = "classTableService")
public class ClassTableServiceImpl implements ClassTableService {

	@Resource(name = "classTableMapper")
	private ClassTableMapper classTableMapper;

	@Override
	public List<ClassTable> queryClassTableList(ClassTable classTable, PageUtil pageUtil) {
		StringBuilder sql = new StringBuilder();
		sql.append(" select * from classTable u where u.flag=0 order by u.updateTime desc ");
		return classTableMapper.queryClassTableList(sql.toString());
	}

	@Override
	public int addClassTable(ClassTable classTable) {
		// TODO Auto-generated method stub
		int id = classTableMapper.addClassTable(classTable);
		return id;
	}

	@Override
	public void updateClassTable(ClassTable classTable) {
		// TODO Auto-generated method stub
		classTableMapper.updateClassTable(classTable);
	}

	@Override
	public void delClassTable(ClassTable classTable) {
		// TODO Auto-generated method stub
		classTableMapper.delClassTable(classTable);
	}

	@Override
	public ClassTable queryClassTableById(Integer id) {
		// TODO Auto-generated method stub
		return classTableMapper.queryClassTableById(id);
	}

	@Override
	public List<ClassTable> queryClassTableList(ClassTable classTable, String beginDate, String endDate) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ct.*,(SELECT COUNT(*) FROM orderclass oc where oc.classTableId=ct.id and oc.status=0) orderClassNum, ");
		sql.append("u.userName classTeacherName,ci.className classInfoName ");
		sql.append("from classtable ct ,classinfo ci ,user u where ct.classInfoId=ci.id and ct.classTeacherId=u.id  and ct.flag=0 ");
		sql.append(" and ct.classDate >='" + beginDate + "'");
		sql.append(" and ct.classDate <='" + endDate + "'");
		sql.append(" and ct.schoolId =" + classTable.getSchoolId() + "");
		sql.append("  order by ct.classStartTime asc ");
		return classTableMapper.queryClassTableList(sql.toString());
	}

	@Override
	public List<OrderClass> queryOrderClassList(OrderClass orderClass) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT oc.*, ");
		sql.append("u.userName studentName ");
		sql.append("from orderClass oc,user u where oc.studentId=u.id  ");
		if (null != orderClass.getClassTableId()) {
			sql.append(" and oc.classTableId =" + orderClass.getClassTableId() + "");
		}

		sql.append(" and oc.flag=0 and oc.status=0 order by oc.createTime asc ");
		return classTableMapper.queryOrderClassList(sql.toString());
	}

	@Override
	public List<ClassTable> queryClassTableListPh(ClassTable classTable, Integer studentId) {
		String now = WeekDateUtil.dateToString(classTable.getClassDate(), "yyyy-MM-dd");
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ct.*,(SELECT COUNT(*) FROM orderclass oc where oc.classTableId=ct.id and oc.status=0 ) orderClassNum,(SELECT COUNT(*) FROM orderclass oc2 where oc2.studentId="
				+ studentId + " and oc2.classTableId=ct.id and oc2.status=0) isOrder, ");
		sql.append("u.userName classTeacherName,ci.className classInfoName ");
		sql.append("from classtable ct ,classinfo ci ,user u where ct.classInfoId=ci.id and ct.classTeacherId=u.id  and ct.flag=0 ");
		sql.append(" and ct.classDate ='" + now + "'");
		sql.append(" and ct.schoolId =" + classTable.getSchoolId() + "");
		sql.append("  order by ct.classStartTime asc ");
		return classTableMapper.queryClassTableList(sql.toString());
	}

	@Override
	public List<ClassTable> queryOrderClassListPh(OrderClass orderClass, String beginDate, String endDate) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ct.*, ");
		sql.append("u.userName classTeacherName,ci.className classInfoName ");
		sql.append("from orderClass oc, classtable ct ,classinfo ci ,user u where oc.classTableId=ct.id  and ct.classInfoId=ci.id and ct.classTeacherId=u.id   ");
		sql.append(" and ct.classDate >='" + beginDate + "'");
		sql.append(" and ct.classDate <='" + endDate + "'");
		sql.append(" and oc.studentId =" + orderClass.getStudentId() + "");
		sql.append(" and oc.schoolId =" + orderClass.getSchoolId() + "");
		sql.append(" and oc.flag=0 and oc.status=0 order by ct.classStartTime asc ");
		return classTableMapper.queryClassTableList(sql.toString());
	}

	@Override
	public List<ClassTable> queryOrderClassList(OrderClass orderClass, String beginDate, String endDate) {
		StringBuilder sql = new StringBuilder();
		sql.append("SELECT ct.classLevel,ct.levelType,ct.classDate,ct.classStartTime,ct.classEndTime,uu.userName studentName, ");
		sql.append("u.userName classTeacherName,ci.className classInfoName,oc.createTime,oc.status orderStatus ");
		sql.append("from orderClass oc, classtable ct ,classinfo ci ,user u,user uu where oc.classTableId=ct.id and oc.studentId=uu.id and ct.classInfoId=ci.id and ct.classTeacherId=u.id   ");
		sql.append(" and oc.studentId =" + orderClass.getStudentId() + "");
		sql.append(" and oc.schoolId =" + orderClass.getSchoolId() + "");
		sql.append(" and oc.flag=0  order by oc.createTime asc ");
		return classTableMapper.queryClassTableList(sql.toString());
	}

	@Override
	public int addOrderClass(OrderClass orderClass) {
		// TODO Auto-generated method stub
		return classTableMapper.addOrderClass(orderClass);
	}

	@Override
	public void updateOrderClass(OrderClass orderClass) {
		// TODO Auto-generated method stub
		classTableMapper.updateOrderClass(orderClass);
	}

}
